home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
FROMUTS
/
UNIXLIB37B
/
test
/
c
/
qtest
< prev
next >
Wrap
Text File
|
1991-01-30
|
1KB
|
92 lines
/* qtest.c - qsort() test program (c) Copyright 1990 H.Rogers */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define A_TYPE int
#define A_SIZE 4096
static int c(const void *i1,const void *i2)
{
return(*(A_TYPE *)i1 - *(A_TYPE *)i2);
}
static clock_t qtest(A_TYPE *a)
{
clock_t t;
t = clock();
qsort(a,A_SIZE,sizeof(A_TYPE),c);
t = clock() - t;
return(t);
}
static void qvrfy(A_TYPE *a)
{
register int i,j;
for (i = 0; i < A_SIZE - 1; )
{
j = a[i];
if (j > a[++i]) goto err;
}
return;
err: printf("not sorted: &a[i] = %#x\ti = %d\n",&a[i],i); exit(1);
}
int main(int argc,char **argv)
{
register A_TYPE *a;
register int i;
int n,j;
clock_t t;
if (argc != 2)
{
puts("usage: qtest ntest");
exit(1);
}
n = atoi(argv[1]);
if (!(a = malloc(sizeof(A_TYPE) * A_SIZE)))
{
perror("qtest");
exit(1);
}
srand(time(0)); t = 0;
for (j = 0; j < n; j++)
{
clock_t q;
for (i = 0; i < (A_SIZE>>1); i++)
a[i] = (A_TYPE)rand() & 0xff;
for (; i < A_SIZE; i++)
a[i] = (A_TYPE)rand();
q = qtest(a);
printf("random data time: %d.%02d\n",q / 100,q % 100);
qvrfy(a);
t += q;
}
t /= n; printf("average: %d.%02d\n",t / 100,t % 100);
for (i = 0; i < A_SIZE; i++)
a[i] = 0;
t = qtest(a);
qvrfy(a);
printf("zero data time: %d.%02d\n",t / 100,t % 100);
}